<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="line-height: 36px;">差旅明细表</span>
      </div>
      <s-table
        :table="table"
        @http="tableWatch">
        <el-table-column
          fixed="right"
          align="center"
          slot="operation"
          class-name="operationMore"
          label="操作"
          width="80">
          <template scope="s">
            <el-button type="primary" icon="el-icon-download" size="mini" @click="download(s.row)" round></el-button>
          </template>
        </el-table-column>
        <el-form-item slot="multiSelect" label="多选操作" prop="multiSelect" v-if="table.multiSelect">
          <el-row>
            <el-col :span="8">
              <el-button size="small" type="primary" @click="download('')">多选导出</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-row>
          <el-col :xs="24" :lg="{span: 11, offset: 0}" class="">
            <el-form-item label="报销人员" prop="reimbursementUser">
              <el-input type="text"
                        class="input"
                        v-model="autoVue_table_reimbursementUser"
                        placeholder="报销人员"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :lg="{span: 11, offset: 1}" class="">
            <el-form-item label="报销单号" prop="reimbursementCode">
              <el-input type="text"
                        class="input"
                        v-model="autoVue_table_reimbursementCode"
                        placeholder="报销单号"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :lg="{span: 11, offset: 0}" class="">
            <el-form-item label="出差人员" prop="travelUsers">
              <el-input type="text"
                        class="input"
                        v-model="autoVue_table_travelUsers"
                        placeholder="出差人员"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :lg="{span: 11, offset: 1}" class="">
            <el-form-item label="出差事由" prop="travelReason">
              <el-input type="text"
                        class="input"
                        v-model="autoVue_table_travelReason"
                        placeholder="出差事由"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :lg="{span: 23, offset: 0}" class="">
            <el-form-item label="报销日期" prop="reimbursementDate">
              <el-date-picker
                v-model="autoVue_table_reimbursementDate"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :lg="{span: 23, offset: 0}" class="">
            <el-form-item prop="departmentId" label="所属部门">
              <el-cascader
                :options="DEPARTMENT.department"
                v-model="autoVue_table_departmentId"
                :show-all-levels="false"
                ref="departmentId"
                clearable
                filterable
                change-on-select
                :props="{
                  value: 'key',
                }"
                placeholder="所属部门"
              ></el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="可用操作" prop="multiSelect">
          <el-row>
            <el-col :span="8">
              <el-button size="small" type="primary" @click="download('')">条件导出</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </s-table>
    </el-card>
  </div>
</template>

<script>
  import {AutoVue, SplitK} from '~/plugins/Tools';

  export default AutoVue({
    data: {
      table: {
        column: [
          {'label': '报销单号',
            search: true,
            'prop': 'reimbursementCode',
            'minWidth': '220px'},
          {'label': '报销人员',
            search: true,
            searchName: 'reimbursementUser',
            'prop': 'reimbursementUserName',
            'minWidth': '0px'},
          {'label': '所属部门',
            search: true,
            searchName: 'departmentId',
            searchAfter: function (value) {
              return value.reverse()[0];
            },
            searchInitValue: function () {
              return [];
            },
            type: 'custom',
            'prop': 'departmentName',
            'minWidth': '150px'},
          {'label': '出差人员',
            search: true,
            'prop': 'travelUsers',
            'minWidth': '200px'},
          {'label': '对应等级', 'prop': 'positionLeave', 'minWidth': '200px'},
          {'label': '出差事由',
            search: true,
            'prop': 'travelReason',
            'minWidth': '0px'},
          {'label': '报销日期',
            'prop': 'reimbursementDate',
            formatter: (row, column) => {
              return this.a.methods.formatterTime(row[column.property || column], 3);
            },
            search: true,
            type: 'DateRange',
            'minWidth': '150px'},
          {'label': '交通费用', 'prop': 'trafficMoneySum', 'minWidth': '0px'},
          {'label': '出差天数', 'prop': 'travelDaySum', 'minWidth': '0px'},
          {'label': '出差补助',
            'prop': 'travelMoneySum',
            'minWidth': '0px',
            formatter: (row, column) => {
              return SplitK(row[column.property || column]);
            }},
          {'label': '其他补助',
            'prop': 'otherMoneySum',
            'minWidth': '0px',
            formatter: (row, column) => {
              return SplitK(row[column.property || column]);
            }},
          {'label': '报销金额',
            'prop': 'moneySum',
            'minWidth': '0px',
            formatter: (row, column) => {
              return SplitK(row[column.property || column]);
            }},
          {'label': '附件数量', 'prop': 'enclosureNum', 'minWidth': '0px'},
        ],
        key: 'id',
        url: '/reports/travelDetails/searchByPage',
        tableColumn: 8,
        popover: {
          width: '660px',
        },
      },
    },
    transition: 'slide-fade-bottom',
    head: {
      'title': '差旅明细表',
      'meta': [
        {'hid': 'description', 'name': 'description', 'content': '乐速科技平台 报表管理 查看差旅明细表'},
      ],
    },
    methods: {
      download (row) {
        let tableRef = 'table';

        let ids = [];
        if (row) {
          ids.push(row.id);
        } else {
          this[tableRef].multipleSelection.forEach(item => {
            ids.push(item.id);
          });
        }
        this.$confirm('您将导出差旅明细表。', '确认导出？', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning',
        })
          .then(async _ => {
            try {
              let option = Object.assign({}, this['autoVue_' + tableRef]);
              await this.p({
                url: '/reports/travelDetails/export',
                method: 'post',
                responseType: 'blob',
                data: {
                  ids,
                  ...option,
                },
              });
            } catch (err) {
              console.log(err);
            }
          })
          .catch(_ => {
          });
      },
    },
    store: ['DEPARTMENT'],
    mounted: async function () {
      this.tableWatch('table');
      this.USE('DEPARTMENT');
      console.log(this);
    },
  });
</script>
